import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ty_smart_watch/common/function_id.dart';
import 'package:ty_smart_watch/page/health/common/data_view.dart';
import 'package:ty_smart_watch/page/health/common/chart_view.dart';
import 'package:ty_smart_watch/page/health/common/max_min_data.dart';
import 'package:ty_smart_watch/page/health/common/select_date_bar.dart';
import 'package:ty_smart_watch/page/health/controller/common_controller.dart';
import 'package:ty_smart_watch/utils/Log.dart';
import 'package:ty_smart_watch/utils/pair.dart';

class HealthChartPage extends StatefulWidget {
  const HealthChartPage({super.key});

  @override
  State<HealthChartPage> createState() => _HealthChartPageState();
}

class _HealthChartPageState extends State<HealthChartPage> {
  int _id = 0;

  final CommonController controller = Get.put(CommonController());

  @override
  void initState() {
    super.initState();
    _id = Get.arguments["id"];
    Log.d("init id : $_id");
  }

  @override
  Widget build(BuildContext context) {
    controller.getDayBarList();
    controller.getDayDetailList();
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text(_getTitle(_id)),
        leading: IconButton(
          icon: const Icon(Icons.arrow_back_ios),
          onPressed: () {
            Get.back();
          },
        ),
      ),
      body: Column(
        children: [
          SelectDateBar(title1: "日", title2: "周", title3: "月", color: _getSelectDateBar(_id)),
          const SizedBox(height: 18),
          Row(
            children: [
              const SizedBox(width: 10),
              Text(_today()),
            ],
          ),
          const SizedBox(height: 8),
          ChartView(
            color: _getColor(_id),
            maxY: _getMaxY(_id),
          ),
          const SizedBox(height: 8),
          Row(
            children: [
              const SizedBox(width: 10),
              Obx(() => Text(_getSubTitle(_id, controller.dateTypeIndex.value))),
            ],
          ),
          const SizedBox(height: 8),
          MaxMinComponent(functionId: _id),
          const SizedBox(height: 8),
          const Expanded(child: DataView(title: "心率")),
        ],
      ),
    );
  }

  String _getTitle(int id) {
    String title = "健康";
    switch (id) {
      case HEART:
        title = "心率";
      case BLOOD_OXYGEN:
        title = "血氧";
      case BLOOD_GLUCOSE:
        title = "血糖";
      case BODY_TEMPERATURE:
        title = "体温";
      case BLOOD_PRESSURE:
        title = "血压";
    }
    return title;
  }
}

String _getSubTitle(int id, int index) {
  String title = "";
  String state = "";
  switch (id) {
    case HEART:
      title = "心率";
    case BLOOD_OXYGEN:
      title = "血氧";
    case BLOOD_GLUCOSE:
      title = "血糖";
    case BODY_TEMPERATURE:
      title = "体温";
    case BLOOD_PRESSURE:
      title = "血压";
  }
  switch (index) {
    case 0:
      state = "今日";
    case 1:
      state = "本周";
    case 2:
      state = "本月";
  }
  return state + title;
}

Pair<Color, Color> _getColor(int id) {
  Pair<Color, Color> mColor = Pair(Colors.red, Colors.blue);
  switch (id) {
    case HEART:
    case BLOOD_OXYGEN:
    case BLOOD_GLUCOSE:
      mColor = Pair(const Color(0xFFFF5F7E), const Color(0xFFFFEEF1));
    case BODY_TEMPERATURE:
      mColor = Pair(const Color(0xFF28BEE0), const Color(0xFFEBF8FB));
    case BLOOD_PRESSURE:
      mColor = Pair(const Color(0xFF157FEB), const Color(0xFFE4EFFF));
  }
  return mColor;
}

double _getMaxY(int id) {
  double max = 0;
  switch (id) {
    case HEART:
    case BLOOD_OXYGEN:
    case BLOOD_GLUCOSE:
    case BODY_TEMPERATURE:
    case BLOOD_PRESSURE:
      max = 200;
  }
  return max;
}

String _getTitle(int id) {
  String title = "健康";
  switch (id) {
    case HEART:
      title = "心率";
    case BLOOD_OXYGEN:
      title = "血氧";
    case BLOOD_GLUCOSE:
      title = "血糖";
    case BODY_TEMPERATURE:
      title = "体温";
    case BLOOD_PRESSURE:
      title = "血压";
  }
  return title;
}

String _today() {
  // 获取当前日期时间
  DateTime now = DateTime.now();

  // 提取年、月、日
  int year = now.year;
  int month = now.month;
  int day = now.day;

  return "$year/$month/$day";
}

Color _getSelectDateBar(int id) {
  Color mColor = Colors.blue;
  switch (id) {
    case HEART:
    case BLOOD_OXYGEN:
    case BLOOD_GLUCOSE:
      mColor = const Color(0xFFFF5F7E);
    case BODY_TEMPERATURE:
      mColor = const Color(0xFF28BEE0);
    case BLOOD_PRESSURE:
      mColor = const Color(0xFF157FEB);
  }
  return mColor;
}
